import { constantRoutes } from '@/router/routes'
import router from '@/router'

const state = {
  routes: [],
  buttons: [],
  addedRoutes: false
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    console.log('设置路由:', routes);
    state.routes = constantRoutes.concat(routes || []);
  },
  SET_BUTTONS: (state, buttons) => {
    state.buttons = buttons
  },
  SET_ADDED_ROUTES: (state, status) => {
    state.addedRoutes = status
  }
}

const actions = {
  // 修改为接收参数的形式
  generateRoutes({ commit }, { role, routes }) {
    return new Promise(resolve => {
      const accessedRoutes = (routes || []).filter(route => !route.hidden);

      console.log('最终可用路由:', accessedRoutes);

      commit('SET_ROUTES', accessedRoutes);
      
      resolve(accessedRoutes);
    })
  }
}

const getters = {
  routes: state => state.routes,
  buttons: state => state.buttons,
  addedRoutes: state => state.addedRoutes
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}